 <?php 

 class accountController extends CI_Controller {

 	public function sign_up()
    {
      /*  $this->load->helper(array('form', 'url'));

		$this->load->library('form_validation');

		$this->form_validation->set_rules('email', 'Email', 'required');
		$this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('number', 'Contact Number', 'trim|required|max_length[11]');
		if ($this->form_validation->run() == FALSE)
		{
           // $this->load->view('sign_in');
             redirect('/Site/?login=true');
        }
        
        else
        {
  */          //file uplaod code
            $config['upload_path'] = './Assets/img/Dp/';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            $this->upload->set_allowed_types('*');
            $data['upload_data'] = '';
            if (!$this->upload->do_upload('userfile')) {
                $data = array('msg' => $this->upload->display_errors());
            }
            else
            { //else, set the success message
                $data = array('msg' => "Upload success!");
                $data['upload_data'] = $this->upload->data();
               //getting file path 
                $file_name = '';
                $file_name .=base_url();
                $file_name .="Assets/img/Dp/";
                $file_name .=$data['upload_data']['file_name'];
            }


            //generating random password
            function RandomString($len = 10)
            {
                $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
                $randstring = '';
                for ($i = 0; $i < $len; $i++) {
                    $randstring = $characters[rand(0,61)];
                }
                return $randstring;
            }
            
            $pass='';
            for($i = 0;$i < 5;$i++)
            {
                //assigning value to variable password
                 $pass .= RandomString();
            }
        //mailing password
    /*     $config = Array(
             'protocol' => 'smtp',
             'smtp_host' => 'mail.baich-day.com',
             'smtp_port' => 465,
             'smtp_user' => 'support@baich-day.com', // change it to yours
             'smtp_pass' => 'admin125maro125', // change it to yours
             'mailtype' => 'html',
             'charset' => 'iso-8859-1',
             'wordwrap' => TRUE
          ); 

          $this->load->library('email', $config);
          $this->email->from('support@baich-day.com', "Admin Team");
          $this->email->to($mail);
          //$this->email->cc("testcc@domainname.com");
          $this->email->subject("Password for BaichDay Account");
          $this->email->message("You have signed up for www.baich-day.com \n The password of your newly made account is:".$pass);
          $this->email->send();

*/  //mailing password end
            $mail='';
            $mail.=$this->input->post('email');
            $mail.="@ucp.edu.pk";
            //form data in an arrray
            $form_data = array(  
                'dp' => $file_name,
                'email' => $mail,
                'pass' => $pass,
                'full_name' => $this->input->post('name'),
                'contact' => $this->input->post('number'),
                'status' => "0",
            );

            $this->load->model('Sign_up');
            $this->Sign_up->sign_up($form_data);
            redirect('/Site/');
//            redirect('/Site?msg=1/');
        
    }
 
     
     public function forgot_pass()
     {
         
         $this->load->model('sign_up');
         $pass=$this->sign_up->forgot_pass($this->input->post('email'));
         //mailing password
    /*     $config = Array(
             'protocol' => 'smtp',
             'smtp_host' => 'mail.baich-day.com',
             'smtp_port' => 465,
             'smtp_user' => 'support@baich-day.com', // change it to yours
             'smtp_pass' => 'admin125maro125', // change it to yours
             'mailtype' => 'html',
             'charset' => 'iso-8859-1',
             'wordwrap' => TRUE
          ); 

          $this->load->library('email', $config);
          $this->email->from('support@baich-day.com', "Admin Team");
          $this->email->to($mail);
          //$this->email->cc("testcc@domainname.com");
          $this->email->subject("Password for BaichDay Account");
          $this->email->message("Password for your www.baich-day.com account is:".$pass);
          $this->email->send();

*/  //mailing password end
        redirect('/Site/');
//      redirect('/Site?msg=0/');
         
     }
     
    
    public function sign_in()
    {
        $this->load->model('Sign_up');
        $result=$this->Sign_up->sign_in($this->input->post('email')."@ucp.edu.pk",$this->input->post('pass'));

        if($result['log_in']==true)
        {
            //session user data
            $newdata = array(
                   'username'  => $result[0]->full_name,
                   'email'     => $result[0]->email,
                   'logged_in' => TRUE,
                   'user_id'   => $result[0]->user_id,
                   'dp' => $result[0]->dp
               ); 

            $this->session->set_userdata($newdata);
            redirect('/members/');
        }
            
        else
            echo "<div class='alert alert-danger' role='alert' style='width:80%; margin-left:10px; margin-top:10px;'>Incorrect Email / Password</div>";
       
    }
    public function check_user_ava()
    {
        $u=$_GET['user'];
        $this->load->model('Sign_up');
        $result=$this->Sign_up->check_user_ava($u);
        if($result['email']==false)
        {
              echo "<div class='alert alert-danger' role='alert' style='width:80%; margin-left:10px; margin-top:10px;'>Email is not Available</div>";
        }
        else
        {
               echo "<div class='alert alert-success' role='alert' style='width:80%; margin-left:10px; margin-top:10px;'>Email is  Available</div>";
        }
    }

    public function log_out()
    {
        $this->session->set_userdata('logged_in',false);
        $this->session->sess_destroy();
        redirect('/site/');
    }

 }